//
//  AnswerViewController.m
//  TenThousand
//
//  Created by 我的 on 16/6/27.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "AnswerViewController.h"
#import "ContentTableViewCell.h"

@interface AnswerViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableDictionary *_showDic;//用来判断分组展开与收缩
}

@end

@implementation AnswerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"问题解答";
    [self answerTableView];
    [self createLeft:[UIImage imageNamed:@"ZHSZicon_03"] leftClick:^{
        [self.navigationController popViewControllerAnimated:YES];
    } right:nil rightClick:nil];
}
- (UITableView *)answerTableView{
    _answerTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height) style:UITableViewStyleGrouped];
    _answerTableView.delegate = self;
    _answerTableView.dataSource = self;
    _answerTableView.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self.view addSubview:_answerTableView];
    return _answerTableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 12;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if ([_showDic objectForKey:[NSString stringWithFormat:@"%ld",section]]) {
        return 1;
    }
        return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (cell == nil) {
        cell = [[ContentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"];
    }
    cell.contentLabel.text = @"1.什么是萬钱宝？什么是萬钱宝？什么是萬钱宝？什么是萬钱宝？什么是萬钱宝？什么是萬钱宝？";
    cell.contentLabel.font = [UIFont systemFontOfSize:13.0f];
    return cell;
}


//添加头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 50)];
    header.backgroundColor = [UIColor whiteColor];
    UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, Screen_Width-40, 40)];
    titleLable.text = [NSString stringWithFormat:@"%ld.什么是萬钱宝？",section];
    titleLable.font = [UIFont systemFontOfSize:14.0f];
    [header addSubview:titleLable];
    
    UIButton *iconBtn = [[UIButton alloc] initWithFrame:CGRectMake(Screen_Width-40, 10, 20, 20)];
    [iconBtn setBackgroundColor:[UIColor redColor]];
    [header addSubview:iconBtn];
    
    header.tag = section;
    
    UITapGestureRecognizer *singleRecongnizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
    singleRecongnizer.numberOfTapsRequired = 1;//点击的次数 = 1：单击
    [singleRecongnizer setNumberOfTapsRequired:1];//一个手指操作
    [header addGestureRecognizer:singleRecongnizer];//添加一个手势监测
    
    
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([_showDic objectForKey:[NSString stringWithFormat:@"%ld",indexPath.section]]) {
        return 70;
    }
    return 0;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 45;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}

/**
 *  获取被点击的分组下标
 *  创建字典，判断该分组字典内键值key=tag的元素是否为1 否则被隐藏
 *  刷新当前的tag所在的分组 实现展开收缩的功能
 */

- (void)singleTap:(UITapGestureRecognizer *)recognizer{
    NSInteger didSection = recognizer.view.tag;
    if (!_showDic) {
        _showDic = [[NSMutableDictionary alloc] init];
    }
    NSString *key = [NSString stringWithFormat:@"%ld",didSection];
    if (![_showDic objectForKey:key]) {
        [_showDic setObject:@"1" forKey:key];
    }else{
        [_showDic removeObjectForKey:key];
    }
    [self.answerTableView reloadSections:[NSIndexSet indexSetWithIndex:didSection] withRowAnimation:UITableViewRowAnimationFade];
}
@end
